package com.yaoandw.util;

import java.util.List;

import com.yaoandw.storage.SimpleCrawlUrl;


/**
 * Utility class that allows for easy string manipulation.  This class will be
 * deprecated once the support for JDK 1.3 will no longer be given, as the
 * functionality delivered by this class is incorporated out-of-the box in
 * Java 1.4 (String class, replace methods, etc ...)
 *
 * $Id: StringUtil.java,v 1.3 2003/05/02 17:36:59 vanrogu Exp $
 *
 * @author G�nther Van Roey ( gunther@javacoding.net )
 */
public class StringUtil {

    /**
     * Replaces the occurences of a certain pattern in a string with a replacement
     * String.
     * @param string the string to be inspected
     * @param pattern the string pattern to be replaced
     * @param replacement the string that should go where the pattern was
     * @return the string with the replacements done
     */
    public static String replace ( String string, String pattern, String replacement ) {
        String replaced = null;

        if (string == null) {
            replaced = null;
        } else if (pattern == null || pattern.length() == 0 ) {
            replaced = string;
        } else {

            StringBuffer sb = new StringBuffer();

            int lastIndex = 0;
            int index = string.indexOf(pattern);
            while (index >= 0) {
                sb.append(string.substring(lastIndex, index));
                sb.append(replacement);
                lastIndex = index + pattern.length();
                index = string.indexOf(pattern, lastIndex);
            }
            sb.append(string.substring(lastIndex));
            replaced = sb.toString();
        }
        return replaced;
    }

    /**
     * @todo add Junit tests for this one
     */
    public static String replace  ( String string, String pattern, String replacement, int start ) {
        String begin = string.substring(0, start);
        String end = string.substring(start);
        return begin + replace(end, pattern, replacement );
    }
    public static boolean isEmpty(String str){
    	return str == null || "".equals(str.trim());
    }
    public static int countChar(char c,String str){
		int num = 0;
		char[] chars = str.toCharArray();
		for(int i = 0; i < chars.length; i++){
		    if(c == chars[i]){
		       num++;
		    }
		}
		return num;
    }

	public static String extractList(List<SimpleCrawlUrl> newUrlList) {
		String rtn = "";
		if(newUrlList != null)
			for(SimpleCrawlUrl newUrl : newUrlList){
				rtn +=newUrl.getOriUrl()+","+newUrl.getLayer()+";";
			}
		return rtn;
	}
}
